package com.rasenhund.android.grecorder.activity;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.rasenhund.android.grecorder.R;
import com.rasenhund.android.grecorder.common.Const;
import com.rasenhund.android.grecorder.dao.RecordDao;
import com.rasenhund.android.grecorder.helper.DbHelper;
import com.rasenhund.android.grecorder.schema.Stage;

/**
 * カテゴリ毎の集計結果表示画面
 */
public class ResultCategoryActivity extends Activity {
    @SuppressWarnings("unused")
    private static final String TAG = ResultCategoryActivity.class.getSimpleName();
    private final ResultCategoryActivity self = this;

    private int mStageId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.result_stage_category);

        getParam();

        initView();

        getStageResult(mStageId);

    }

    /**
     * パラメータの取得
     */
    void getParam() {

        Intent intent = getIntent();

        if (intent != null) {
            mStageId = intent.getIntExtra(Const.KEY.KEY_STAGE, -1);
        }

        if (mStageId == -1) {
            finishActivity(RESULT_CANCELED);
        }

    }

    /**
     * Viewの初期化
     */
    private void initView() {

        View view;
        TextView textView;

        view = (LinearLayout) findViewById(R.id.lstGrapp);
        textView = (TextView) view.findViewById(R.id.stageNm);
        textView.setText(R.string.category_grapp);

        view = (LinearLayout) findViewById(R.id.lstProx);
        textView = (TextView) view.findViewById(R.id.stageNm);
        textView.setText(R.string.category_prox);

        view = (LinearLayout) findViewById(R.id.lstShoot);
        textView = (TextView) view.findViewById(R.id.stageNm);
        textView.setText(R.string.category_shoot);

        view = (LinearLayout) findViewById(R.id.lstCannon);
        textView = (TextView) view.findViewById(R.id.stageNm);
        textView.setText(R.string.category_cannon);

        view = (LinearLayout) findViewById(R.id.lstSnipe);
        textView = (TextView) view.findViewById(R.id.stageNm);
        textView.setText(R.string.category_snip);

    }

    /**
     * 戦績の取得
     * 
     * @param stage
     */
    private void getStageResult(int stageId) {

        ArrayList<Stage> list = null;

        DbHelper dbHelper = new DbHelper(self);
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        RecordDao dao = new RecordDao(db);

        list = dao.getResultByCategory(stageId);

        db.close();

        View view = null;
        Stage stage = null;

        for (int i = 0; i < list.size(); i++) {
            stage = list.get(i);
            stage.calcPerWin();

            switch (stage.getCategoryCd()) {
                case Const.CATEGORY.GRAPP:
                    view = (LinearLayout) findViewById(R.id.lstGrapp);
                    break;
                case Const.CATEGORY.PROX:
                    view = (LinearLayout) findViewById(R.id.lstProx);
                    break;
                case Const.CATEGORY.SHOOT:
                    view = (LinearLayout) findViewById(R.id.lstShoot);
                    break;
                case Const.CATEGORY.CANNON:
                    view = (LinearLayout) findViewById(R.id.lstCannon);
                    break;
                case Const.CATEGORY.SNIPE:
                    view = (LinearLayout) findViewById(R.id.lstSnipe);
                    break;
                default:
                    break;
            }

            ((TextView) view.findViewById(R.id.totalCount)).setText(
                    String.valueOf(stage.getTotalCount()));
            ((TextView) view.findViewById(R.id.winCount)).setText(
                    String.valueOf(stage.getWinCount()));
            ((TextView) view.findViewById(R.id.loseCount)).setText(
                    String.valueOf(stage.getLoseCount()));
            ((TextView) view.findViewById(R.id.drawCount)).setText(
                    String.valueOf(stage.getDrawCount()));
            ((TextView) view.findViewById(R.id.perWin)).setText(
                    String.valueOf(stage.getPerWin()));

        }

    }

}
